﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OverStore.Runtime.ObjectRepository.Attacher;
using OverStore.Exceptions.Configuration;
using OverStore.Runtime.PersistenceServiceProvider;

namespace OverStore.Configuration.ObjectRepository.Attacher
{
    /// <summary>
    /// Provide method for creating instances of <see cref="DelegateAttacher{TInstance, TIdentifier}"/> class.
    /// </summary>
    /// <typeparam name="TInstance">The type of the instance.</typeparam>
    /// <typeparam name="TIdentifier">The type of the identifier.</typeparam>
    public class DelegateAttacherConfiguration<TInstance, TIdentifier> : IComponentConfiguration<IAttacher<TInstance, TIdentifier>> 
        where TInstance : class
    {
        /// <summary>
        /// Gets or sets the provide attach info.
        /// </summary>
        public Action<TInstance, IPersistenceServiceProvider ,InstanceAttachInfo<TIdentifier>> ProvideAttachInfo { get; set; }

        /// <summary>
        /// Creates the new instance of type using set of services provided by <paramref name="context"/> object.
        /// </summary>
        /// <param name="context">The context provides access to services shared between components.</param>
        /// <returns>
        /// The new instance of class.
        /// </returns>
        public IAttacher<TInstance, TIdentifier> CreateComponent(OverStore.Runtime.OverStoreContext.IOverStoreContext context)
        {
            if (ProvideAttachInfo == null)
                throw this.ConfigurationException("ProvideAttachInfo delegate is missing.");
            return new DelegateAttacher<TInstance, TIdentifier>(context.PersistenceServiceProvider, ProvideAttachInfo);
        }
    }
}
